package experiment2;

import java.util.Arrays;

/**
 * @author mazouri
 * @create 2021-10-09 11:21
 */
public class Permutation {
    public void permute(int[] nums) {
        if (nums == null) return;

        dfs(0, nums);
    }

    private void dfs(int idx, int[] nums) {
        // 不能再往下搜索
        if (idx == nums.length) {
            System.out.println(Arrays.toString(nums));
            return;
        }

        // 枚举这一层所有可以做出的选择
        for (int i = idx; i < nums.length; i++) {
            swap(nums, idx, i);
            dfs(idx + 1, nums);
            swap(nums, idx, i);
        }
    }

    private void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }

    public static void main(String[] args) {
        new Permutation().permute(new int[]{1, 2, 3});
    }
}
